package com.mongodb;

import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.mongodb.entity.User;
import com.mongodb.repository.UserRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;

@SpringBootTest
class MongoDbApplicationTests1 {

    // 注入mongoTemplate
    @Autowired
    private UserRepository userRepository;

    // 注入MongoDB Template
    @Autowired
    private MongoTemplate mongoTemplate;

    // 添加操作
    @Test
    void create() {
        User user = new User();
        user.setAge(10);
        user.setName("曹子阳");
        user.setEmail("1321@qq.com");
        User save = userRepository.save(user);
        System.out.println(save);
    }

    @Test
    // 查询所以记录
    public void findAll() {
        List<User> all = userRepository.findAll();
        System.out.println(all);
    }

    @Test
    // id查询
    public void findId() {
        User user = userRepository.findById("617ff536744bcf093a064fd2").get();
        System.out.println(user);
    }

    @Test
    // 条件查询
    public void FindUserList() {
        // name = 曹子阳 and  age=10
        User user = new User();
        user.setName("曹子阳");
        user.setAge(10);
        Example example = Example.of(user);
        List all = userRepository.findAll(example);
        System.out.println(all);
    }


    @Test
    // 模糊查询
    public void FindLikeUserList() {
        // name = 曹子阳 and  age=10


        // 设置模糊查询匹配规则
        ExampleMatcher matcher =  ExampleMatcher.matching().
                withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        User user = new User();
        user.setName("曹");
        user.setAge(10);

        Example example = Example.of(user,matcher);
        List all = userRepository.findAll(example);
        System.out.println(all);
    }


    @Test
    // 分页查询
    public void FindPageUserList() {
        // 分页
        // 0 代表第一页
        Pageable pageable = PageRequest.of(0, 3);


        User user = new User();
        user.setName("曹");

        Example<User> example = Example.of(user);
        Page<User> page = userRepository.findAll(example, pageable);
        System.out.println(page);
    }


    @Test
    // 修改
    public void updateUser() {
        User user = userRepository.findById("617f6ba61c2edb47c85d9bc4").get();
        user.setAge(100);
        user.setName("1231312132123123");
        User save = userRepository.save(user);
        System.out.println(save);

    }

    // 删除
    @Test
    public void deleteUser() {
        userRepository.deleteById("617f6ba61c2edb47c85d9bc4");
    }
}
